//
//  QuestionViewController.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "QuestionViewController.h"
#import "QuestionModel.h"
#import "QuestionTableViewCell.h"
#import "GetDataHandle.h"
#import "Url_marco.h"

#import "QuestionDetalisViewController.h"
#import "MBProgressHUD.h"
#import "QuestionDetalisViewController.h"
#import <MJRefresh.h>

@interface QuestionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *QuestionTableView;

@property (strong,nonatomic) NSMutableArray *QuestionArray;

@property (assign,nonatomic) NSInteger currentPage;



@end

@implementation QuestionViewController

//懒加载
- (NSMutableArray *)QuestionArray{
    if (_QuestionArray == nil) {
        _QuestionArray = [NSMutableArray array];
    }
    return _QuestionArray;
}


- (void)viewWillAppear:(BOOL)animated{
self.tabBarController.tabBar.hidden = NO;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    

    
    self.QuestionTableView.delegate = self;
    self.QuestionTableView.dataSource = self;
    
    [self.QuestionTableView registerNib:[UINib nibWithNibName:@"QuestionTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
   

    //    下拉加载
    self.QuestionTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.QuestionArray removeAllObjects];
        [self getSession];
    }];
    [self.QuestionTableView.mj_header beginRefreshing];
    
//    上拉刷新
    self.QuestionTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self DropgetSession];
    }];
    
//    取消tableview 中的换行线
    self.QuestionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    

    
    

}






//数据解析
- (void)getSession{

    
[[GetDataHandle sharedDataHandle]getDataWithUrlString:QUESTION_URL withGetDataBlock:^(NSData *data) {
    NSError *error = nil;
    NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSDictionary *dic = di[@"data"];
    NSArray *array = dic[@"expertList"];
    for (NSDictionary *dict in array) {
        QuestionModel *model = [QuestionModel new];
        [model setValuesForKeysWithDictionary:dict];
        [self.QuestionArray addObject:model];
    }
    
    NSString *str = di[@"message"];
    if ([str isEqualToString:@"操作成功"]) {
        
        [self.QuestionTableView.mj_header endRefreshing];
        [self.QuestionTableView reloadData];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
//        [self.QuestionTableView.av_footer endFooterRefreshing];

        [self.QuestionTableView reloadData];
    });
}];
    
}

//上拉加载
- (void)DropgetSession{
    
    self.currentPage += 10;
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/expert/classification/health/%ld-10.html",self.currentPage];
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:str withGetDataBlock:^(NSData *data) {
        NSError *error = nil;
        NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        NSDictionary *dic = di[@"data"];
        NSArray *array = dic[@"expertList"];
        for (NSDictionary *dict in array) {
            QuestionModel *model = [QuestionModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.QuestionArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.QuestionTableView.mj_footer endRefreshing];
            [self.QuestionTableView reloadData];
        });
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.QuestionArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    QuestionModel *model = self.QuestionArray[indexPath.row];
    cell.model = model;

    
//    //        点击cell不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    [cell NightWithType:UIViewColorTypeNormal];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 240;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionModel *model = self.QuestionArray[indexPath.row];
    QuestionDetalisViewController *ques = [QuestionDetalisViewController new];
    ques.stringID = model.expertId;
    [self showViewController:ques sender:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
